﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BeatSpawner : MonoBehaviour
{
    public EChannel eChannel;
    public EBeatType eBeatType;
    public bool isAutoTrigger = true;
    public GameObject BeatPrefab_Regular;
    public GameObject BeatPrefab_Little;
    public GameObject BeatPrefab_HighHeel;
    public int SpawnedCount = 0;
    public int currentBeatTime_MS = 0;

    private void Awake() 
    {
        BeatPrefab_Regular = Resources.Load(Global.PREFAB_PATH_FOOTSTEP_REGULAR) as GameObject;
        BeatPrefab_Little = Resources.Load(Global.PREFAB_PATH_FOOTSTEP_LITTLE) as GameObject;
        BeatPrefab_HighHeel = Resources.Load(Global.PREFAB_PATH_FOOTSTEP_HIGHHEEL) as GameObject;
    }

    public void SpawnBeat(float newBeatBeforeTriggerDuration, BeatConfig newBeatsConfig = null)
    {
        GameObject BeatGo = null;

        if (eBeatType == EBeatType.Regular_L)
        {
            BeatGo = Instantiate(BeatPrefab_Regular, new Vector3(999,999,999), Quaternion.identity);
            BeatGo.name = "Regular_L";
        }
        if (eBeatType == EBeatType.Regular_R)
        {
            BeatGo = Instantiate(BeatPrefab_Regular, new Vector3(999,999,999), Quaternion.identity);
            BeatGo.name = "Regular_R";
            Vector3 srcScale = BeatGo.transform.localScale;
            BeatGo.transform.localScale = new Vector3(-srcScale.x, srcScale.y, srcScale.z);
        }
        if (eBeatType == EBeatType.Little_L)
        {
            BeatGo = Instantiate(BeatPrefab_Little, new Vector3(999,999,999), Quaternion.identity);
            BeatGo.name = "Little_L";
        }
        if (eBeatType == EBeatType.Little_R)
        {
            BeatGo = Instantiate(BeatPrefab_Little, new Vector3(999,999,999), Quaternion.identity);
            BeatGo.name = "Little_R";
            Vector3 srcScale = BeatGo.transform.localScale;
            BeatGo.transform.localScale = new Vector3(-srcScale.x, srcScale.y, srcScale.z);
        }
        if (eBeatType == EBeatType.HighHeel_L)
        {
            BeatGo = Instantiate(BeatPrefab_HighHeel, new Vector3(999,999,999), Quaternion.identity);
            BeatGo.name = "HighHeel_L";
        }
        if (eBeatType == EBeatType.HighHeel_R)
        {
            BeatGo = Instantiate(BeatPrefab_HighHeel, new Vector3(999,999,999), Quaternion.identity);
            BeatGo.name = "HighHeel_R";
            Vector3 srcScale = BeatGo.transform.localScale;
            BeatGo.transform.localScale = new Vector3(-srcScale.x, srcScale.y, srcScale.z);
        }

        Vector3 StartPosition = BeatManager.Instance.GetChannelSpawnPos(eChannel);
        Vector3 EndPosition = BeatManager.Instance.GetChannelTargetPos(eChannel);
        BeatGo.GetComponent<Beat>().Init(StartPosition, EndPosition, newBeatBeforeTriggerDuration, isAutoTrigger, (int)eChannel % 2 == 1 ? KeyCode.Q : KeyCode.E);
        SpawnedCount ++;
        
        if(newBeatsConfig != null)
        {
            for(int i = 0; i < newBeatsConfig.BeatModelList.Count; i++)
            {
                if(newBeatsConfig.BeatModelList[i].Type == eBeatType && newBeatsConfig.BeatModelList[i].BeatTime > currentBeatTime_MS)     //  间隔一帧跳过
                {
                    int nextBeatTime_MS = newBeatsConfig.BeatModelList[i].BeatTime;
                    int nextBeatDeltaTime_MS = nextBeatTime_MS - currentBeatTime_MS;
                    float nextBeatDeltaTime = nextBeatDeltaTime_MS / 1000.0f;
                    Timer.Register(nextBeatDeltaTime, () => {SpawnBeat(newBeatBeforeTriggerDuration, newBeatsConfig);});
                    currentBeatTime_MS = nextBeatTime_MS;
                    break;
                }
            }
        }
    }

    public void DelaySpawnBeat(float firstBeatTime, float newBeatBeforeTriggerDuration, BeatConfig newBeatsConfig = null)
    {
        currentBeatTime_MS = (int)(firstBeatTime * 1000);
        Timer.Register(firstBeatTime, ()=>{SpawnBeat(newBeatBeforeTriggerDuration, newBeatsConfig);});
    }
}
